<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of promotion
 *
 * @author Gidaff_01
 */
class Promotion extends Admin_Controller{
    //put your code here
    function Promotion() {
        parent::Admin_Controller();
        $this->load->model('Promotion_m');
        $this->load->library('pagination');
        //$this->load->library('upload');
        $this->load->library('session');
        $this->load->helper('date');

        $this->_data['erreur'] = "";
    }

    function index() {
        $this->liste_promotion();
    }

    function liste_promotion(){
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/promotion/liste_promotion/' . '/' . $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Promotion_m->count_all_promotion();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['commandes'] = $this->Promotion_m->get_all_promotion($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Promotion_m->count_all_promotion();
        $this->_data['page_title'] = "Liste de promotion";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('promotion/liste_promotion');
    }

    function delete_promotion($id, $per_page, $off_set) {
        $this->Promotion_m->delete_promotion($id);
        redirect('promotion/liste_promotion' . '/' . $per_page . '/' . $off_set);
    }

    function ajouter_promotion(){
         if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'code',
                    'Label' => 'Code',
                    'rules' => 'required|integer|callback_check_code'
                ),
                array(
                    'field' => 'pct',
                    'Label' => 'Pour cent',
                    'rules' => 'required|numeric'
                ),
                
                array(
                    'field' => 'type',
                    'Label' => 'Type',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            if ($this->form_validation->run() == TRUE) {
                    $data = array(
                        'code' => $this->input->post('code'),
                        'pct' => $this->input->post('pct'),
                        'type'=>$this->input->post('type')
                    );
                    $this->Promotion_m->ajouter_promotion($data);
                    redirect('promotion/liste_promotion');
            } else {
                $this->display_view('promotion/ajouter_promotion');
            }
        } else {
            $this->display_view('promotion/ajouter_promotion');
        }
    }

    function check_code() {
        $code = $this->input->post('code');
        $query = $this->Promotion_m->is_code_existe($code);
        if ($query->num_rows() > 0) {
            $this->form_validation->set_message('check_code', 'The code was exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function update_promotion($promotion_id){
        is_admin();
        $this->_data['page_title'] = "Update Promotion";
        $promotion = $this->Promotion_m->get_promotion_by_id($promotion_id);
        $this->_data['detail_promotion'] = $promotion;
         if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'code',
                    'Label' => 'Code',
                    'rules' => 'required|integer'
                ),
                array(
                    'field' => 'pct',
                    'Label' => 'Pour cent',
                    'rules' => 'required|numeric'
                ),
               
                array(
                    'field' => 'type',
                    'Label' => 'Type',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            if ($this->form_validation->run() == TRUE) {
                    $data = array(
                        'code' => $this->input->post('code'),
                        'pct' => $this->input->post('pct'),
                       'type'=>$this->input->post('type')
                    );
                    $this->Promotion_m->update_promotion($data,$promotion_id);
                    redirect('promotion/liste_promotion');
            } else {
                $this->display_view('promotion/update_promotion');
            }
        } else {
            $this->display_view('promotion/update_promotion');
        }
    }
}
?>
